package net.wicp.tams.common.redis;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import net.wicp.tams.common.Conf;
import net.wicp.tams.common.apiext.StringUtil;
import net.wicp.tams.common.redis.builder.RedisSentinelBuilderTams;
import net.wicp.tams.common.redis.builder.RedisStandaloneBuilderTams;
import net.wicp.tams.common.redis.pool.AbsPool;
import net.wicp.tams.common.redis.pool.JedisPoolTams;
import net.wicp.tams.common.redis.pool.JedisSentinelPoolTams;
import redis.clients.jedis.Jedis;

public abstract class RedisAssit {
	public static JedisPoolTams standalone(String host, int port, GenericObjectPoolConfig<Jedis> poolConfig) {
		GenericObjectPoolConfig<Jedis> poolConfigtrue = poolConfig == null ? initDefaultConfig() : poolConfig;
		host = StringUtil.hasNull(host, Conf.get("common.redis.redisserver.host"));
		port = port <= 0 ? Conf.getInt("common.redis.redisserver.port") : port;
		RedisStandaloneBuilderTams build = RedisStandaloneBuilderTams.builder().host(host).port(port)
				.timeout(Conf.getInt("common.redis.redisserver.timeout")).poolConfig(poolConfigtrue).build();
		return build.getJedisPoolTams();
	}

	public static JedisPoolTams standalone() {
		return standalone(null, 0, null);
	}

	public static JedisSentinelPoolTams sentinel(String masterName, String[] sentinelAry,
			GenericObjectPoolConfig<Jedis> poolConfig) {
		GenericObjectPoolConfig<Jedis> poolConfigtrue = poolConfig == null ? initDefaultConfig() : poolConfig;
		Set<String> sentinels = new HashSet<>();
		for (String sentinel : sentinelAry) {
			String[] sentinelArr = sentinel.split(":");
			if (sentinelArr.length == 2) {
				sentinels.add(sentinel);
			}
		}
		RedisSentinelBuilderTams build = RedisSentinelBuilderTams.builder().masterName(masterName).sentinels(sentinels)
				.poolConfig(poolConfigtrue).build();
		return build.getJedisSentinelPoolTams();
	}

	public static JedisSentinelPoolTams sentinel(String masterName, String[] sentinelAry) {
		return sentinel(masterName, sentinelAry, null);
	}
	
	
	public static AbsPool getPool() {
		String masterName =  Conf.get("common.redis.redisserver.masterName");
		String sentinelAry =  Conf.get("common.redis.redisserver.sentinelAry");
		AbsPool retpool=null;
		if(StringUtil.isNotNull(masterName)&&StringUtil.isNotNull(sentinelAry)) {
			String[] sentinelArys = sentinelAry.split(",");
			retpool = sentinel(masterName,sentinelArys);
		}else {
			retpool=standalone();
		}
		return retpool;
	}

	private static GenericObjectPoolConfig<Jedis> initDefaultConfig() {
		GenericObjectPoolConfig<Jedis> poolConfig = new GenericObjectPoolConfig<>();
		poolConfig.setMaxWaitMillis(Conf.getInt("common.redis.redisserver.maxWaitMillis"));
		poolConfig.setMaxTotal(Conf.getInt("common.redis.redisserver.maxTotal"));
		poolConfig.setMaxIdle(Conf.getInt("common.redis.redisserver.maxIdle"));
		poolConfig.setTestOnBorrow(Conf.getBoolean("common.redis.redisserver.testOnBorrow"));
		poolConfig.setTestWhileIdle(Conf.getBoolean("common.redis.redisserver.testWhileIdle"));
		poolConfig.setMinIdle(Conf.getInt("common.redis.redisserver.minIdle"));
		poolConfig
				.setSoftMinEvictableIdleTimeMillis(Conf.getInt("common.redis.redisserver.minEvictableIdleTimeMillis"));
		poolConfig.setSoftMinEvictableIdleTimeMillis(
				Conf.getInt("common.redis.redisserver.softMinEvictableIdleTimeMillis"));
		poolConfig.setNumTestsPerEvictionRun(Conf.getInt("common.redis.redisserver.numTestsPerEvictionRun"));
		poolConfig.setTestOnReturn(Conf.getBoolean("common.redis.redisserver.testOnReturn"));
		poolConfig.setTimeBetweenEvictionRunsMillis(
				Conf.getInt("common.redis.redisserver.timeBetweenEvictionRunsMillis"));
		poolConfig.setBlockWhenExhausted(Conf.getBoolean("common.redis.redisserver.blockWhenExhausted"));
		return poolConfig;
	}

}
